﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Web;

namespace ASPNETPerformance.App_BusinessLogic.ViewState {
    public class ViewStateCompression_Deflate {

        public ViewStateCompression_Deflate()
        {
        }

        // 压缩
        public static byte[] Compress( byte[] Data ) {
            MemoryStream MemStream = new MemoryStream();
            DeflateStream DefStream = new DeflateStream( MemStream, CompressionMode.Compress, true );
            DefStream.Write( Data, 0, Data.Length );
            DefStream.Close();
            return Data.ToArray();
        }

        // 解压
        public static byte[] Decompress( byte[] Data ) {
            MemoryStream MemStream = new MemoryStream();
            DeflateStream DefStream = new DeflateStream( new MemoryStream( Data ), CompressionMode.Decompress, true );
            byte[] Buffer = new byte[ 4096 ];
            int Size;
            while ( true ) {
                Size = DefStream.Read( Buffer, 0, Buffer.Length );
                if ( Size > 0 )
                    MemStream.Write( Buffer, 0, Size );
                else
                    break;
            }
            DefStream.Close();
            return MemStream.ToArray();
        }
    }
}
